package com.ai.demo.picture;

/**
 * AUTHER: wangyue
 * TIME  : 2025/3/17 : 16:09
 */
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ImageStitcher {

    /**
     * 拼接图片的方法
     * @param imagePaths 图片文件路径列表
     * @param direction 拼接方向，"horizontal" 表示水平拼接，"vertical" 表示垂直拼接
     * @param outputPath 拼接后图片的输出路径
     * @throws IOException 如果读取或写入图片时发生错误
     */
    public static void stitchImages(List<String> imagePaths, String direction, String outputPath) throws IOException {
        if (imagePaths == null || imagePaths.isEmpty()) {
            throw new IllegalArgumentException("图片路径列表不能为空");
        }

        // 读取所有图片并存储在 BufferedImage 列表中
        List<BufferedImage> images = new ArrayList<>();
        int maxWidth = 0;
        int totalHeight = 0;
        int maxHeightInRow = 0;

        for (String path : imagePaths) {
            BufferedImage image = ImageIO.read(new File(path));
            images.add(image);
            if (direction.equals("horizontal")) {
                maxWidth = Math.max(maxWidth, image.getWidth());
                totalHeight += image.getHeight();
            } else if (direction.equals("vertical")) {
                totalHeight = Math.max(totalHeight, image.getHeight());
                maxHeightInRow += image.getWidth();
            }
        }

        // 根据拼接方向计算新图片的宽度和高度
        int stitchedImageWidth;
        int stitchedImageHeight;
        if (direction.equals("horizontal")) {
            stitchedImageWidth = maxWidth;
            stitchedImageHeight = totalHeight;
        } else if (direction.equals("vertical")) {
            stitchedImageWidth = maxHeightInRow;
            stitchedImageHeight = totalHeight;
        } else {
            throw new IllegalArgumentException("不支持的拼接方向，仅支持 'horizontal' 或 'vertical'");
        }

        // 创建新的 BufferedImage 对象
        BufferedImage stitchedImage = new BufferedImage(stitchedImageWidth, stitchedImageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = stitchedImage.createGraphics();

        // 绘制每张图片到新图片上
        int y = 0;
        int x = 0;
        for (BufferedImage image : images) {
            if (direction.equals("horizontal")) {
                g2d.drawImage(image, x, y, null);
                x += image.getWidth();
            } else if (direction.equals("vertical")) {
                g2d.drawImage(image, x, y, null);
                y += image.getHeight();
            }
        }

        g2d.dispose();

        // 保存拼接后的图片
        File output = new File(outputPath);
        ImageIO.write(stitchedImage, "jpg", output);
    }

    public static void main(String[] args) {
        List<String> imagePaths = new ArrayList<>();
        // 添加需要拼接的图片路径
//        imagePaths.add("path/to/image1.jpg");
//        imagePaths.add("path/to/image2.jpg");
//        imagePaths.add("path/to/image3.jpg");

        imagePaths.add("E:\\HuaweiMoveData\\Users\\wangyue\\Desktop\\jiajiao\\banner1.jpg");
        imagePaths.add("E:\\HuaweiMoveData\\Users\\wangyue\\Desktop\\jiajiao\\banner2.jpg");
        imagePaths.add("E:\\HuaweiMoveData\\Users\\wangyue\\Desktop\\jiajiao\\banner3.jpg");



        String direction = "horizontal"; // 可以改为 "vertical" 进行垂直拼接
//        String outputPath = "path/to/stitched_image.jpg";
        String outputPath = "E:\\HuaweiMoveData\\Users\\wangyue\\Desktop\\jiajiao\\stitched_image.jpg";

        try {
            stitchImages(imagePaths, direction, outputPath);
            System.out.println("图片拼接成功，保存路径：" + outputPath);
        } catch (IOException e) {
            System.err.println("图片拼接失败：" + e.getMessage());
        }
    }
}
